package leetcode.每日一题;

import org.junit.Test;

import java.util.*;

/**
 * @author ：zsy
 * @date ：Created 2022/1/19 10:24
 * @description：https://leetcode-cn.com/problems/contains-duplicate-ii/
 */
public class 存在重复元素II {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.containsNearbyDuplicate(new int[]{1,2, 1}, 0));
    }

    class Solution {
        public boolean containsNearbyDuplicate(int[] nums, int k) {
            if (k == 0) return false;
            Set<Integer> set = new HashSet<>();
            for (int i = 0; i < nums.length; i++) {
                if (i > k) {
                    set.remove(nums[i - k - 1]);
                }
                if (!set.add(nums[i])) return true;
            }
            return false;
        }
    }
}
